package com.tim.app.share;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.application.library.log.DLOG;
import com.application.library.util.StringUtil;
import com.tim.app.R;
import com.tim.app.server.net.ServerAddressManager;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMWeb;


/**
 * Created by guodong on 2016/4/21 17:22.
 */
public class UMShareUtil {
    private Activity mContext;
    private UMShareAPI mShareAPI = null;
    private UMShareListener shareListener;

    public UMShareUtil(Activity context, UMShareListener shareListener) {
        this.mContext = context;
        this.shareListener = shareListener;
        mShareAPI = UMShareAPI.get(context);
    }

    public void shareWeb(SHARE_MEDIA platform, String title, String desc, String imageUrl, String targetUrl) {
        if (platform == SHARE_MEDIA.SINA) {
            shareImage(platform, title, desc, imageUrl);
            return;
        }
        UMWeb umWeb = new UMWeb(targetUrl);
        if (!TextUtils.isEmpty(title)) {
            umWeb.setTitle(title);
        }
        if (!TextUtils.isEmpty(desc)) {
            umWeb.setDescription(desc);
        }
        if (TextUtils.isEmpty(imageUrl)) {
            imageUrl = ServerAddressManager.SHARE_DEFAULT_LOGO_URL;
        }
        DLOG.d("share_image_url==" + imageUrl);
        UMImage umImage = new UMImage(mContext, imageUrl);
        umWeb.setThumb(umImage);
        ShareAction shareAction = new ShareAction(mContext);
        shareAction.withMedia(umWeb);
        shareAction.setPlatform(platform);
        mShareAPI.doShare(mContext, shareAction, shareListener);
    }

    public void shareWeb(SHARE_MEDIA platform, String title, String desc, Bitmap imageBitmap, String targetUrl) {
        if (platform == SHARE_MEDIA.SINA) {
            shareImage(platform, title, desc, imageBitmap);
            return;
        }
        UMWeb umWeb = new UMWeb(targetUrl);
        if (!TextUtils.isEmpty(title)) {
            umWeb.setTitle(title);
        }
        if (!TextUtils.isEmpty(desc)) {
            umWeb.setDescription(desc);
        }
        if (imageBitmap == null) {
            imageBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher);
        }
        //水印
//        UMImageMark umImageMark = new UMImageMark();
//        umImageMark.setGravity(Gravity.BOTTOM | Gravity.RIGHT);
//        umImageMark.setMarkBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher));
//        umImageMark.setAlpha(0.5f);//设置透明度
//        umImageMark.setMargins(5, 5, 5, 5);//设置边距
        UMImage umImage = new UMImage(mContext, imageBitmap);
        umWeb.setThumb(umImage);
        ShareAction shareAction = new ShareAction(mContext);
        shareAction.withMedia(umWeb);
        shareAction.setPlatform(platform);
        mShareAPI.doShare(mContext, shareAction, shareListener);

    }

    public void shareVideo(SHARE_MEDIA platform, String title, String desc, String videoUrl) {
        UMVideo umVideo = new UMVideo(videoUrl);
        if (!StringUtil.isEmpty(title)) {
            umVideo.setTitle(title);
        }
        if (!StringUtil.isEmpty(desc)) {
            umVideo.setDescription(desc);
        }
        ShareAction shareAction = new ShareAction(mContext);
        shareAction.withMedia(umVideo);
        shareAction.setPlatform(platform);
        mShareAPI.doShare(mContext, shareAction, shareListener);
    }

    public void shareLink(SHARE_MEDIA platform, String title, String desc, String targetUrl) {
        UMWeb umWeb = new UMWeb(targetUrl);
        if (!StringUtil.isEmpty(title)) {
            umWeb.setTitle(title);
        }
        if (!StringUtil.isEmpty(desc)) {
            umWeb.setDescription(desc);
        }
        ShareAction shareAction = new ShareAction(mContext);
        shareAction.withMedia(umWeb);
        shareAction.setPlatform(platform);
        mShareAPI.doShare(mContext, shareAction, shareListener);
    }

    public void shareImage(SHARE_MEDIA platform, String title, String desc, String imageUrl) {
        UMImage umImage = new UMImage(mContext, imageUrl);
        if (!TextUtils.isEmpty(title)) {
            umImage.setTitle(title);
        }
        if (!TextUtils.isEmpty(desc)) {
            umImage.setDescription(desc);
        }
        ShareAction shareAction = new ShareAction(mContext);
        shareAction.withMedia(umImage);
        if (platform == SHARE_MEDIA.SINA && !TextUtils.isEmpty(desc)) {
            shareAction.withText(desc);
        }
        shareAction.setPlatform(platform);
        mShareAPI.doShare(mContext, shareAction, shareListener);
    }

    public void shareImage(SHARE_MEDIA platform, String title, String desc, Bitmap imageBitmap) {
        if (imageBitmap == null) {
            imageBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher);
        }
        UMImage umImage = new UMImage(mContext, imageBitmap);
        if (!TextUtils.isEmpty(title)) {
            umImage.setTitle(title);
        }
        if (!TextUtils.isEmpty(desc)) {
            umImage.setDescription(desc);
        }
        ShareAction shareAction = new ShareAction(mContext);
        shareAction.withMedia(umImage);
        if (platform == SHARE_MEDIA.SINA && !TextUtils.isEmpty(desc)) {
            shareAction.withText(desc);
        }
        shareAction.setPlatform(platform);
        mShareAPI.doShare(mContext, shareAction, shareListener);
    }

    public boolean checkWXInstalled() {
        return mShareAPI.isInstall(mContext, SHARE_MEDIA.WEIXIN);
    }
}
